﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using LLoger;


namespace LimitatorInterfaces.UnloadServices
{
    public class UnloadService : ServiceBase, IUnloadService
    {
        private readonly IUnloadProvider _unloadProvider;
        private readonly IUnloadFormatter _formatter;
        private readonly List<LimitEntity> _sourceLimits = new List<LimitEntity>();
        
        public void UnloadLimits()
        {
            foreach (var lim in _sourceLimits)
            {
                _unloadProvider.AddLimit(_formatter.Format(lim));
            }

            object param = GetStartWithSettingValues(Res.UnloadProviderParam);
            
            _unloadProvider.UnloadLimits(param);
        }

        public UnloadService(IUnloadProvider unloadProvider, IUnloadFormatter formatter, string configPath, ILogWriter logWriter)
            : base(configPath, logWriter)
        {
            _unloadProvider = unloadProvider;
            _formatter = formatter;
        }

        public void LoadSourceLimits(List<LimitEntity> sourceLimits)
        {
            if (sourceLimits == null || sourceLimits.Count <1) throw new ArgumentException("Таблица исходных лимитов пуста или не инициализирована","sourceLimits");
            
            sourceLimits.ForEach(e => _sourceLimits.Add(e.Clone()));
        }


    }
}
